﻿using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Utilities.Json
{
    public class NewtonJson
    {
        // Serialize
        public static string Serialize(object @object, string dateTimeFormat)
        {
            try
            {
                return JsonConvert.SerializeObject(@object, new IsoDateTimeConverter { DateTimeFormat = dateTimeFormat });
            }
            catch (Exception ex)
            {
                //Logger.WriteLog(Logger.LogType.Error, ex.Message);
                return string.Empty;
            }
        }

        public static string Serialize(object @object)
        {
            try
            {
                return JsonConvert.SerializeObject(@object, Formatting.None, MicrosoftDateFormatSettings);
            }
            catch (Exception ex)
            {
                //Logger.WriteLog(Logger.LogType.Error, ex.Message);
                return string.Empty;
            }
        }

        // DeSerialize
        public static object Deserialize(string jsonString)
        {
            try
            {
                return JsonConvert.DeserializeObject(jsonString);
            }
            catch (Exception ex)
            {
                //Logger.WriteLog(Logger.LogType.Error, ex.Message);
                return null;
            }
        }
        public static T Deserialize<T>(string jsonString)
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(jsonString, MicrosoftDateFormatSettings);
            }
            catch (Exception ex)
            {
                //Logger.WriteLog(Logger.LogType.Error, ex.Message);
                return default(T);
            }
        }
        public static T Deserialize<T>(string jsonString, string dateTimeFormat)
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(jsonString, new IsoDateTimeConverter { DateTimeFormat = dateTimeFormat });
            }
            catch (Exception ex)
            {
                //Logger.WriteLog(Logger.LogType.Error, ex.Message);
                return default(T);
            }
        }

        // Privates Format Date
        public enum DateFormatHandling
        {
            IsoDateFormat,
            MicrosoftDateFormat
        }

        private static readonly JsonSerializerSettings MicrosoftDateFormatSettings = new JsonSerializerSettings();

        /*
            09  LogEntry entry = new LogEntry
            10  {
            11    LogDate = new DateTime(2009, 2, 15, 0, 0, 0, DateTimeKind.Utc),
            12    Details = "Application started."
            13  };
            14
            15  // default as of Json.NET 4.5
            16  string isoJson = JsonConvert.SerializeObject(entry);
            17  // {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}
            18
            19  JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
            20  {
            21    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
            22  };
            23  string microsoftJson = JsonConvert.SerializeObject(entry, microsoftDateFormatSettings);
            24  // {"Details":"Application started.","LogDate":"\/Date(1234656000000)\/"}
            25
            26  string javascriptJson = JsonConvert.SerializeObject(entry, new JavaScriptDateTimeConverter());
            27  // {"Details":"Application started.","LogDate":new Date(1234656000000)}
         */
    }
}
